﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using Controller.ShakeGestures;

namespace OmnipotentFlashlight
{
    public partial class screen_change : PhoneApplicationPage
    {
        DispatcherTimer timer = null;
        int i = 0;
        string action = "";
        Color temColor;
        Color temColor2;
        Color temColor3 = Colors.Transparent;

        public screen_change()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(screen_change_Loaded);

            action = Common.GetSetting("change_mode");
            action = action == null ? "bianse" : action;

            if (action == "caihong")
            {
                icon_flash.Visibility = Visibility.Collapsed;
                CH.Visibility = Visibility.Visible;
            }
            else 
            {
                icon_flash.Visibility = Visibility.Visible;
                CH.Visibility = Visibility.Collapsed;
            }

        }

        void screen_change_Loaded(object sender, RoutedEventArgs e)
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(action == "bianse" ? 1000 : 200);
            if (action == "jingdeng")
            {
                timer.Tick += new EventHandler(timer_Tick_jingdeng);
            }
            else if (action == "caihong")
            {
                timer.Tick += new EventHandler(timer_Tick_caihong);
            }
            else
            {
                timer.Tick += new EventHandler(timer_Tick);
            }
            timer.Start();
        }
        void timer_Tick_caihong(object sender, EventArgs e)
        {
            i++;
            if (i == 1)
            {
                temColor = (CH1.Background as SolidColorBrush).Color;
                temColor3 = (temColor3 == Colors.Transparent) ? Common.HtmlToColor("#FF3399") : temColor3;
                SetCanvs(CH1, temColor3);
            }
            else if (i == 2)
            {
                temColor2 = (CH2.Background as SolidColorBrush).Color;
                SetCanvs(CH2, temColor);
            }
            else if (i == 3)
            {
                temColor = (CH3.Background as SolidColorBrush).Color;
                SetCanvs(CH3, temColor2);
            }
            else if (i == 4)
            {
                temColor2 = (CH4.Background as SolidColorBrush).Color;
                SetCanvs(CH4, temColor);
            }
            else if (i == 5)
            {
                temColor = (CH5.Background as SolidColorBrush).Color;
                SetCanvs(CH5, temColor2);
            }
            else if (i == 6)
            {
                temColor2 = (CH6.Background as SolidColorBrush).Color;
                SetCanvs(CH6, temColor);
            }
            else if (i == 7)
            {
                temColor3 = (CH7.Background as SolidColorBrush).Color;
                SetCanvs(CH7, temColor2);
                i = 0;
            }
        }

        void timer_Tick_jingdeng(object sender, EventArgs e)
        {
            i++;
            if (i == 1)
            {
                SetColor(Colors.Red);
            }
            else if (i == 2)
            {
                SetColor(Colors.White);
            }
            else if (i == 3)
            {
                SetColor(Colors.Blue);
                i = 0;
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            i++;
            if (i == 1)
            {
                SetColor(Colors.Red);
            }
            else if (i == 2)
            {
                SetColor(Colors.White);
            }
            else if (i == 3)
            {
                SetColor(Colors.Yellow);
            }
            else if (i == 4)
            {
                SetColor(Colors.Green);
            }
            else if (i == 5)
            {
                SetColor(Common.HtmlToColor("#CCFFFF"));
            }
            else if (i == 6)
            {
                SetColor(Colors.Blue);
            }
            else if (i == 7)
            {
                SetColor(Colors.Gray);
                i = 0;
            }
        }

        void SetColor(Color col)
        {
            ContentPanel.Background = new SolidColorBrush(col);
        }

        void SetCanvs(Canvas can, Color col)
        {
            can.Background = new SolidColorBrush(col);
        }

    }
}
